package cn.xsw.controller;

import cn.xsw.utils.IResult;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.List;

public class BaseController {

    public IResult result;

    public BaseController() {
        this.result = new IResult();
    }

    // 参数校验
    public boolean check(BindingResult checkResult) {
        if (checkResult.hasErrors()) {
            List<FieldError> fieldErrors = checkResult.getFieldErrors();
            StringBuilder sb = new StringBuilder();
            fieldErrors.forEach(fieldError -> {
                sb.append("[" + fieldError.getField() + "]" + fieldError.getDefaultMessage() + ";");
            });
            this.result.webError(sb.toString());
            return true;
        }
        return false;
    }
}
